package ru.marketsoft.reminder.threads;

import ru.marketsoft.reminder.frames.IFrame;
import ru.marketsoft.reminder.tools.AePlayWave;
import ru.marketsoft.reminder.tools.Settings;
import ru.marketsoft.reminder.tools.Utils;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.TimerTask;

/**
 * User: dimka3210
 * Date: 28.02.13
 * Time: 22:02
 */
public class IVisible implements Runnable {
    private IFrame frame;
    java.util.Timer timer2 = new java.util.Timer();
    TimerTask task = new TimerTask() {
        public void run() {
            try {
                System.out.println(Utils.getReminderHours(1) + "===" + Utils.getHours() + "===" + Utils.getReminderHours(2) + "===" + Utils.getMinutes());
                if (Utils.getReminderHours(1) == Utils.getHours() && Utils.getReminderHours(2) == Utils.getMinutes()) {
                    if (Dump.isNewRunnable()) {
                        JLabel label = Dump.getjLabel();
                        label.setText(Settings.getInstance().getProperty("message"));
                        frame.setVisible(true);

                        if ("on".equals(Settings.getInstance().getProperty("sound"))) {
                            File forPath = new File("ru/marketsoft/reminder/sounds/" + Settings.getInstance().getProperty("sound_item"));
                            String soundSelectedPath = forPath.getAbsolutePath();
                            AePlayWave aw = new AePlayWave(soundSelectedPath);
                            aw.start();
                        }

                        Dump.setNewRunnable(false);
                        Dump.runColorOverFlow();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    };

    public IVisible(IFrame frame) {
        this.frame = frame;
    }

    /*@Override
    public void run() {
        while (true) {
            try {
                System.out.println(Utils.getReminderHours(1) + "===" + Utils.getHours() + "===" + Utils.getReminderHours(2) + "===" + Utils.getMinutes());
                if (Utils.getReminderHours(1) == Utils.getHours() && Utils.getReminderHours(2) == Utils.getMinutes()) {
                    if (Dump.isNewRunnable()) {
                        frame.setVisible(true);
                        Dump.setNewRunnable(false);
                        Dump.runColorOverFlow();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }*/

    @Override
    public void run() {
        timer2.schedule(task, 0, 60000);
    }
}
